const  crypto = require('crypto');
const axios = require('axios');
const request = require('request-promise');



/**
* 翻译器
*@function setConfig(config)
*@function getConfig() return Object
*@function md5() return String
*@function getRandomN() return number
*@function generateUrlParams return String
*@function translate
*/
class Translator{
    //
    #config={
        from:'',
        to:'',
        appKey:'',
        secretKey:''
    }

    /**
     * @generator 
     */
    constructor(){

    }

    /**
     * @param {Object} config 
     * config{
        from:String,
        to:String,
        appKey:String,
        secretKey:String
     * }
     */
    setConfig(config){
        if(typeof config !="object") throw new Error("the params must be a object");
        this.#config = config;
    }
    /**
     * @returns {Object} config
     */
    getConig(){
        return this.#config;
    }
    #md5=(str)=>{
        let hamc = crypto.createHash('md5');
        hamc.update(str);
        return hamc.digest('hex');
    }
    #getRandomN=(roundTo)=> {
        return Math.round(Math.random() * roundTo);
    }
    #generateUrlParams=(_params)=>{
        const paramsData = [];
        for (const key in _params) {
          if (_params.hasOwnProperty(key)) {
            paramsData.push(key + '=' + _params[key]);
          }
        }
        const result = paramsData.join('&');
        return result;
    }
    /**
     *@param {string} word
     *@returns {Array} data
     */
    translate(word){
        return new Promise((resolve,reject)=>{
            let host = "http://openapi.youdao.com/api";//有道词典api
            let salt = this.#getRandomN(1000);
            let sign = this.#md5(this.#config.appKey+word+salt+this.#config.secretKey);
            let target = encodeURI(word);
            let params ={
                q: target,
                from: this.#config.from,
                to: this.#config.to,
                appKey: this.#config.appKey,
                salt: salt,
                sign: sign
            };
            let url = host+'?'+this.#generateUrlParams(params);
            axios.get(url).then(data=>{
                resolve(data);
            }).catch(error=>{
                reject(error);
            })
        })
    }
    /**
     *@param {string} word
     *@returns {string} data
     */
    async translateSync(word){
        let host = "http://openapi.youdao.com/api";//有道词典api
        let salt = this.#getRandomN(1000);
        let sign = this.#md5(this.#config.appKey+word+salt+this.#config.secretKey);
        let target = encodeURI(word);
        let params ={
            q: target,
            from: this.#config.from,
            to: this.#config.to,
            appKey: this.#config.appKey,
            salt: salt,
            sign: sign
        };
        let url = host+'?'+this.#generateUrlParams(params);
        let result = await request.get(url);
        result = JSON.parse(result);
        let data;
        if(result["errorCode"]=="0"){
            data = result["translation"][0];
        }else{

        }
        return data;
    }
}


module.exports = Translator;